package gui;

import houseHold.House;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.xml.transform.TransformerConfigurationException;

import locations.LocationArchive;

import org.xml.sax.SAXException;

import action.ActionPool;

import person.PersonAchive;
import reader.ScenarioBuilder;
import simulation.SimRunner;
import toXML.LoadFamilyXML;
import toXML.LoadHouseXML;
import toXML.SaveFamilyXML;
import toXML.SaveHouseXML;
import RoomPanel.Aproach;
import RoomPanel.PanelBuilder;
import RoomPanel.RoomPanel;
import constants.Constants;
import done.DBConnection;

public class MainGUI extends JFrame {

	private static JLabel FamilyStatus = null;
	private static JLabel HouseStatus = null;
	private static MainGUI instance = new MainGUI();
	private static JLabel ScenarioStatus = null;
	private static final long serialVersionUID = 1L;
	private static JLabel SimulationStatus = null;

	public static MainGUI getInstance() {
		return instance;
	}

	public static void setFamilyStatus(Color c, String msg) {
		FamilyStatus.setText(msg);
		FamilyStatus.setForeground(c);
	}

	public static void setHouseStatus(Color c, String msg) {
		HouseStatus.setText(msg);
		HouseStatus.setForeground(c);
	}

	public static void setScenarioStatus(Color c, String msg) {
		ScenarioStatus.setText(msg);
		ScenarioStatus.setForeground(c);
	}
	
	public static void setSimulationStatus(Color c, String msg) {
		SimulationStatus.setText(msg);
		SimulationStatus.setForeground(c);
	}

	private JCheckBox CheckVisual = null;
	private JButton CreateFamily = null;
	private JButton EditFamily = null;
	private JButton EditHouse = null;
	private JPanel FamilyPanel = null;
	private JButton FamilyValidate = null;
	private JButton ScenarioMenu = null;
	private JPanel HousePanel = null;
	private JButton HouseValidate = null;
	private JPanel jContentPane = null;
	private JButton LoadFamily = null;
	private JButton LoadHouse = null;
	private JButton NewHouse = null;
	private JButton RunSimulation = null;
	private JButton SaveFamily = null;
	private JButton SaveHouse = null;
	private static JButton ScenarioValidate = null;
	private JPanel ScenarioPanel = null;

	private JPanel SimulationPanel = null;

	private JProgressBar SimulationProgress = null;

	private JButton SImulationResults = null;

	private JLabel VisualSim = null;

	/**
	 * This is the default constructor
	 */
	private MainGUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes CheckVisual
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getCheckVisual() {
		if (CheckVisual == null) {
			CheckVisual = new JCheckBox();
			CheckVisual.setBounds(new Rectangle(130, 120, 25, 25));
			CheckVisual.setBackground(new Color(250, 250, 250));
		}
		return CheckVisual;
	}

	/**
	 * This method initializes CreateFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreateFamily() {
		if (CreateFamily == null) {
			CreateFamily = new JButton();
			CreateFamily.setBounds(new Rectangle(20, 90, 110, 25));
			CreateFamily.setForeground(new Color(0, 0, 102));
			CreateFamily.setText("New Family");
			CreateFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					PersonAchive.reset();
					FamilyDialog fd = new FamilyDialog(getInstance());
					getInstance().setEnabled(false);
					fd.setVisible(true);
				}
			});
		}
		return CreateFamily;
	}

	/**
	 * This method initializes EditFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEditFamily() {
		if (EditFamily == null) {
			EditFamily = new JButton();
			EditFamily.setBounds(new Rectangle(20, 160, 90, 25));
			EditFamily.setForeground(new Color(0, 0, 102));
			EditFamily.setText("Edit");
			EditFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FamilyDialog fd = new FamilyDialog(getInstance());
					getInstance().setEnabled(false);
					fd.setVisible(true);
				}
			});
		}
		return EditFamily;
	}

	/**
	 * This method initializes EditHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEditHouse() {
		if (EditHouse == null) {
			EditHouse = new JButton();
			EditHouse.setBounds(new Rectangle(20, 160, 90, 25));
			EditHouse.setForeground(new Color(0, 0, 102));
			EditHouse.setText("Edit");
			EditHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					prepare();
					HouseEditor.getInstance().setVisible(true);
					MainGUI.getInstance().setEnabled(false);
				}
			});
		}
		return EditHouse;
	}

	/**
	 * This method initializes FamilyPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getFamilyPanel() {
		if (FamilyPanel == null) {
			FamilyStatus = new JLabel();
			FamilyStatus.setBounds(new Rectangle(15, 30, 140, 30));
			FamilyStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setFamilyStatus(Constants.red,"Family Inactive");
			FamilyPanel = new JPanel();
			FamilyPanel.setLayout(null);
			FamilyPanel.setBounds(new Rectangle(210, 10, 170, 280));
			FamilyPanel.setBackground(new Color(250, 250, 250));
			FamilyPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Family",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));

			FamilyPanel.add(getCreateFamily(), null);
			FamilyPanel.add(getLoadFamily(), null);
			FamilyPanel.add(getEditFamily(), null);
			FamilyPanel.add(getSaveFamily(), null);
			FamilyPanel.add(FamilyStatus, null);
			FamilyPanel.add(getFamilyValidate(), null);
		}
		return FamilyPanel;
	}

	/**
	 * This method initializes FamilyValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getFamilyValidate() {
		if (FamilyValidate == null) {
			FamilyValidate = new JButton();
			FamilyValidate.setBounds(new Rectangle(20, 120, 110, 25));
			FamilyValidate.setForeground(new Color(0, 0, 102));
			FamilyValidate.setText("Validate");
			FamilyValidate
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if(PersonAchive.getCount()>0)
								setFamilyStatus(Constants.green, "Family Active");
							else setFamilyStatus(Constants.red, "Family Inactive");
						}
					});
		}
		return FamilyValidate;
	}

	/**
	 * This method initializes ScenarioMenu
	 * 
	 * @return javax.swing.JButton
	 */
	ScenarioDialog sd = null;
	private JButton EditScenario = null;

	private JButton getScenarioMenu() {
		if (ScenarioMenu == null) {
			ScenarioMenu = new JButton();
			ScenarioMenu.setBounds(new Rectangle(20, 90, 110, 25));
			ScenarioMenu.setForeground(new Color(0, 0, 102));
			ScenarioMenu.setEnabled(true);
			ScenarioMenu.setText("Build new");
			ScenarioMenu.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
						LocationArchive.init();
						sd = new ScenarioDialog(MainGUI.getInstance());

					sd.setVisible(true);
				}
			});
		}
		return ScenarioMenu;
	}

	/**
	 * This method initializes HousePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getHousePanel() {
		if (HousePanel == null) {
			HouseStatus = new JLabel();
			HouseStatus.setBounds(new Rectangle(15, 30, 140, 30));
			HouseStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setHouseStatus(Constants.red, "House Inactive");
			HousePanel = new JPanel();
			HousePanel.setLayout(null);
			HousePanel.setBounds(new Rectangle(10, 10, 170, 280));
			HousePanel.setBackground(new Color(250, 250, 250));
			HousePanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "House",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));
			HousePanel.add(HouseStatus, null);
			HousePanel.add(getNewHouse(), null);
			HousePanel.add(getLoadHouse(), null);
			HousePanel.add(getEditHouse(), null);
			HousePanel.add(getSaveHouse(), null);

			HousePanel.add(getHouseValidate(), null);
			HousePanel.add(getNewHouse(), null);

		}
		return HousePanel;
	}

	/**
	 * This method initializes HouseValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getHouseValidate() {
		if (HouseValidate == null) {
			HouseValidate = new JButton();
			HouseValidate.setBounds(new Rectangle(20, 120, 110, 25));
			HouseValidate.setForeground(new Color(0, 0, 102));
			HouseValidate.setText("Validate");
			HouseValidate
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							Aproach.computeCount(HouseEditor.panels);
							if (House.getInstance().isActive()) {
								try {
									SaveHouseXML.save(Constants.activeFile);
								} catch (FileNotFoundException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (TransformerConfigurationException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (SAXException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								setHouseStatus(Constants.green, "House Active");

							} else
								setHouseStatus(Constants.red, "House Inactive");
						}
					});
		}
		return HouseValidate;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.white);
			jContentPane.add(getHousePanel(), null);
			jContentPane.add(getFamilyPanel(), null);
			jContentPane.add(getScenarioPanel(), null);
			jContentPane.add(getSimulationPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes LoadFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadFamily() {
		if (LoadFamily == null) {
			LoadFamily = new JButton();
			LoadFamily.setBounds(new Rectangle(20, 190, 90, 25));
			LoadFamily.setForeground(new Color(0, 0, 102));
			LoadFamily.setText("Load");
			LoadFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try{
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						LoadFamilyXML.loadXML(file);
					}
					}
					catch(Exception e1)
					{
						setFamilyStatus(Constants.red, "Load error");
					}
					setFamilyStatus(Constants.red, "Family loaded");

				}
			});
		}
		return LoadFamily;
	}

	/**
	 * This method initializes LoadHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoadHouse() {
		if (LoadHouse == null) {
			LoadHouse = new JButton();
			LoadHouse.setBounds(new Rectangle(20, 190, 90, 25));
			LoadHouse.setForeground(new Color(0, 0, 102));
			LoadHouse.setText("Load");
			LoadHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try{
					House.reset();
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						LoadHouseXML.loadXML(file);
					}
					prepare();
					}
					catch(Exception e1)
					{
						setHouseStatus(Constants.red, "Load error");
					}
					setHouseStatus(Constants.red, "House Loaded");
					
				}
			});
		}
		return LoadHouse;
	}

	/**
	 * This method initializes NewHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getNewHouse() {
		if (NewHouse == null) {
			NewHouse = new JButton();
			NewHouse.setBounds(new Rectangle(20, 90, 110, 25));
			NewHouse.setForeground(new Color(0, 0, 102));
			NewHouse.setText("New House");
			NewHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					House.reset();
					HouseEditor.panels.clear();
					HouseEditor.getInstance().RoomBuilder.removeAll();
					HouseEditor.getInstance().setVisible(true);
					MainGUI.getInstance().setEnabled(false);
				}
			});
		}
		return NewHouse;
	}

	/**
	 * This method initializes RunSimulation
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRunSimulation() {
		if (RunSimulation == null) {
			RunSimulation = new JButton();
			RunSimulation.setBounds(new Rectangle(20, 90, 130, 25));
			RunSimulation.setForeground(new Color(0, 0, 102));
			RunSimulation.setEnabled(true);
			RunSimulation.setText("Run Simulation");
			RunSimulation.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setSimulationStatus(Constants.green,"Setting up DB");
					DBConnection.initConnection();
					DBConnection.CreateTables();
					DBConnection.insertMainData();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					SimRunner.runSimulation("");
					setSimulationStatus(Constants.green, "Simulation running");
				}
			});
		}
		return RunSimulation;
	}

	/**
	 * This method initializes SaveFamily
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveFamily() {
		if (SaveFamily == null) {
			SaveFamily = new JButton();
			SaveFamily.setBounds(new Rectangle(20, 220, 90, 25));
			SaveFamily.setForeground(new Color(0, 0, 102));
			SaveFamily.setText("Save");
			SaveFamily.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						try {
							SaveFamilyXML.save(file);
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (TransformerConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SAXException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			});
		}
		return SaveFamily;
	}

	/**
	 * This method initializes SaveHouse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSaveHouse() {
		if (SaveHouse == null) {
			SaveHouse = new JButton();
			SaveHouse.setBounds(new Rectangle(20, 220, 90, 25));
			SaveHouse.setForeground(new Color(0, 0, 102));
			SaveHouse.setText("Save");
			SaveHouse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(MainGUI.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						try {
							SaveHouseXML.save(file);
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (TransformerConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SAXException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			});
		}
		return SaveHouse;
	}

	/**
	 * This method initializes ScenarioValidate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getScenarioValidate() {
		if (ScenarioValidate == null) {
			ScenarioValidate = new JButton();
			ScenarioValidate.setBounds(new Rectangle(20, 120, 110, 25));
			ScenarioValidate.setForeground(new Color(0, 0, 102));
			ScenarioValidate.setText("Validate");
			ScenarioValidate.setEnabled(false);
			ScenarioValidate.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ActionPool.clear();
					for(int i=0;i<ScenarioDialog.scenarios.size();i++){
						ScenarioBuilder sb=new ScenarioBuilder();
						sb.build(ScenarioDialog.scenarios.get(i));
					}
					setScenarioStatus(Constants.green,"Scenario Active");
					
				}
			});
		}
		return ScenarioValidate;
	}

	/**
	 * This method initializes ScenarioPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getScenarioPanel() {
		if (ScenarioPanel == null) {
			ScenarioStatus = new JLabel();
			ScenarioStatus.setBounds(new Rectangle(15, 30, 140, 30));
			ScenarioStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setScenarioStatus(Constants.red, "Scenario Inactve");
			ScenarioPanel = new JPanel();
			ScenarioPanel.setLayout(null);
			ScenarioPanel.setBounds(new Rectangle(410, 10, 170, 280));
			ScenarioPanel.setBackground(new Color(250, 250, 250));
			ScenarioPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Scenario",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));

			ScenarioPanel.add(getScenarioMenu(), null);
			ScenarioPanel.add(ScenarioStatus, null);
			ScenarioPanel.add(getScenarioValidate(), null);
			ScenarioPanel.add(getEdit(), null);
		}
		return ScenarioPanel;
	}

	/**
	 * This method initializes SimulationPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getSimulationPanel() {
		if (SimulationPanel == null) {
			SimulationStatus = new JLabel();
			SimulationStatus.setBounds(new Rectangle(15, 30, 140, 30));
			SimulationStatus.setHorizontalAlignment(SwingConstants.CENTER);
			setSimulationStatus(Constants.red, "Simulation Inactve");
			VisualSim = new JLabel();
			VisualSim.setBounds(new Rectangle(20, 120, 103, 25));
			VisualSim.setForeground(new Color(0, 84, 128));
			VisualSim.setText("Visual Simulation:");
			SimulationPanel = new JPanel();
			SimulationPanel.setLayout(null);
			SimulationPanel.setBounds(new Rectangle(610, 10, 170, 280));
			SimulationPanel.setBackground(new Color(250, 250, 250));
			SimulationPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Simulation",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));

			SimulationPanel.add(getRunSimulation(), null);
			SimulationPanel.add(getSImulationResults(), null);
			SimulationPanel.add(VisualSim, null);
			SimulationPanel.add(getCheckVisual(), null);
			SimulationPanel.add(SimulationStatus, null);
			SimulationPanel.add(getSimulationProgress(), null);
		}
		return SimulationPanel;
	}

	/**
	 * This method initializes SimulationProgress
	 * 
	 * @return javax.swing.JProgressBar
	 */
	public void setProgressBarValue(int value)
	{
		SimulationProgress.setValue(value);
	}
	
	private JProgressBar getSimulationProgress() {
		if (SimulationProgress == null) {
			SimulationProgress = new JProgressBar();
			SimulationProgress.setBounds(new Rectangle(20, 180, 130, 25));
			
		}
		return SimulationProgress;
	}

	/**
	 * This method initializes SImulationResults
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSImulationResults() {
		if (SImulationResults == null) {
			SImulationResults = new JButton();
			SImulationResults.setBounds(new Rectangle(20, 220, 130, 25));
			SImulationResults.setForeground(new Color(0, 0, 102));
			SImulationResults.setEnabled(false);
			SImulationResults.setText("Get Results");
		}
		return SImulationResults;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(810, 350);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	private void prepare() {
		HouseEditor.panels.clear();
		HouseEditor.getInstance().RoomBuilder.removeAll();
		for (int i = 0; i < House.getInstance().getRoomCount(); i++) {
			RoomPanel p = PanelBuilder.newPanel(House.getInstance().getRoom(i));
			p.addPopUpMenu();
			HouseEditor.panels.add(p);
			HouseEditor.getInstance().RoomBuilder.add(p);
		}
		Aproach.computeCount(HouseEditor.panels);
	}

	/**
	 * This method initializes Clear
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEdit() {
		if (EditScenario == null) {
			EditScenario = new JButton();
			EditScenario.setBounds(new Rectangle(20, 160, 90, 25));
			EditScenario.setForeground(new Color(0, 0, 102));
			EditScenario.setText("Edit");
			EditScenario.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (sd == null) {
						LocationArchive.init();
						sd = new ScenarioDialog(MainGUI.getInstance());
					}
					sd.setVisible(true);
				}
			});
		}
		return EditScenario;

	}

	public static void ActivateSV(boolean value) {
		System.out.println(value);
		ScenarioValidate.setEnabled(value);
		ScenarioValidate.repaint();
		
	}

} // @jve:decl-index=0:visual-constraint="10,10"
